package com.dongbao.core.rules.java;

import com.dongbao.core.base.CodeRulesCommonBase;
import com.dongbao.core.rules.CodeRulesBase;
import com.dongbao.dialog.DHGToolWindow;
import com.dongbao.entity.AwardedInfo;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiClassImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  同一个service注入了多次同一个DAO,Controller同理(刘弦)
 * */
public class RepeatImportRules extends CodeRulesCommonBase implements CodeRulesBase {

    private static final Logger LOGGER = Logger.getInstance(DHGToolWindow.class);

    public RepeatImportRules(VirtualFile afterFile,PsiFile psiFile,Project project) {
        super(afterFile,psiFile,project);
        this.code = "B011";
        this.level = "B";
    }

    /**
     * 执行规则判断
     * */
    @Override
    public void run(List<AwardedInfo> deduction) {
        /* 执行分析当前class是否负责当前规则 */
        PsiAnnotation[] annotations = psiClassImpls.get(0).getModifierList().getAnnotations();
        for(PsiClassImpl psiClass:psiClassImpls){
            /* 该规则只对Controller 和 Service有效 */
            if(!psiClass.getName().contains("ServiceImpl") && !psiClass.getName().contains("Controller")){
                continue;
            }
            /* 获取类中所有的变量 */
            List<PsiField> ownFields = new ArrayList(psiClass.getOwnFields());
            if(psiClass.getSuperClass()!=null){
                for(PsiField superField:psiClass.getSuperClass().getFields()){
                    ownFields.add(superField);
                }
            }
//            Map<PsiClass, List<PsiField>> typeCollect = Arrays.stream(allFields).collect(Collectors.groupingBy(PsiField::getContainingClass));
            Map<String, List<PsiField>> typeCollect = ownFields.stream().collect(Collectors.groupingBy(item->item.getType().getCanonicalText()));
            for(String keyClass:typeCollect.keySet()){
                if(keyClass.contains("com.dongbao")){
                    List<PsiField> psiFields = typeCollect.get(keyClass);
                    if(psiFields.size()>1){
                        LOGGER.info("666");
                        /* 获取错误代码行位置 */
                        Document document = PsiDocumentManager.getInstance(psiClass.getProject()).getDocument(psiFields.get(0).getContainingClass().getContainingFile());
                        int lineNumber = document.getLineNumber(psiFields.get(0).getTextOffset());

                        /* 判断方法内的代码是否都是赋值的操作 */
                        packDeduction(
                                deduction,
                                psiFields.get(0).getContainingClass().getContainingFile().getVirtualFile(),
                                psiFields.get(0).getContainingClass().getName(),
                                psiPackageStatements.get(0).getPackageName(),
                                "",
                                "在【"+psiFields.get(0).getContainingClass().getName()+"】中重复添加了【"+psiFields.get(0).getType().getCanonicalText()+"】的注入,使用的对象名是【"+psiFields.get(0).getName()+"】",lineNumber,lineNumber);
                    }
                }
            }
        }
    }

    @Override
    public String describe() {
        return "补充";
    }
}
